// #include <stdio.h>

// #include "SDL/SDL_log.h"
// #include "SDL/SDL.h"
// #include "SDL/SDL_image.h"
// #include "sprite.h"

// #define WINDOW_HEIGHT 800
// #define WINDOW_WIDHT 600

// SDL_Window *window;
// SDL_Renderer *render;

// Sprite* sprite;

// void draw(){
//     //使用render绘制白色背景
//     SDL_SetRenderDrawColor(render, 255, 255, 255, 255);
//     SDL_RenderClear(render);

//     //开始绘制
//     if(sprite == NULL){
//        createSprite(&sprite, render, 0, 200);
//     }
//     showSprite(sprite, render);
//   //  destroySprite(sprite2);
//     //结束绘制

//     SDL_RenderPresent(render);
//     SDL_UpdateWindowSurface(window);
// }

// void event_loop(){
//  //必须有事件处理，否则窗体出不来，
//     while (1)
//     {
//        draw();
//        SDL_Event event;
//        while(SDL_PollEvent(&event)){
//            // printf("event is %#x\n", event.type); // test code
//             switch (event.type)
//             {
//                 case SDL_MOUSEMOTION:

//                    SDL_Log("move...%d", event.motion.x);
//                    sprite->posX = event.motion.x;
//                    sprite->posY = event.motion.y;
//                    break;
//                 case SDL_MOUSEBUTTONDOWN:
//                    sprite->posX = event.button.x;
//                    sprite->posY = event.button.y;
//                    break;
//                 case SDL_MOUSEBUTTONUP:
//                    break;      
//                 case SDL_QUIT:
//                     return;
                
//                 default:
//                     break;
//             }
//             if (SDL_QUIT == event.type) {
//                 return;
//             }
//        }
//     }
// }

// int showWindow(){
//     if(SDL_Init(SDL_INIT_VIDEO)){
//        SDL_Log("init error : %s", SDL_GetError());
//        return 1;
//     }
//     window = SDL_CreateWindow("title", 
//                             SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
//                             WINDOW_WIDHT, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
//     if(window == NULL){
//        SDL_Log("create window error %s", SDL_GetError());
//        return 2;
//     }

//     //创建图形渲染器
//     render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
//     if(render == NULL){
//        SDL_Log("create render error %s", SDL_GetError());
//     }
//     //事件循环
//     event_loop();
    
//     SDL_DestroyRenderer(render);
//     SDL_DestroyWindow(window);
// }

// int main()
// {
//     showWindow();
//     return 0;
// }


#define SDL_MAIN_HANDLED
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "SDL/SDL_log.h"
#define W 800
#define H 600
#define SPEED 90

int main(int argc, char *argv[]) {
	int i, j;
	int done = 0;
	SDL_Event event;
	SDL_Window *win = NULL;
//	SDL_Surface *surface = NULL;
	SDL_Surface *img = NULL;
	SDL_Renderer *rend = NULL;
	SDL_Texture *texture = NULL;
	SDL_Rect rect, walks[8];
 
	SDL_Init(SDL_INIT_VIDEO);
	win = SDL_CreateWindow("DNF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
		W, H, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
//	surface = SDL_GetWindowSurface(win);
	rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED| SDL_RENDERER_PRESENTVSYNC);
	img = IMG_Load("walk.png");
	if (img == NULL)
	{
		printf("error\n");
		system("pause");
		return 0;
	}
	texture = SDL_CreateTextureFromSurface(rend, img);
	rect.x = 0;
	rect.y = 0;
	rect.h = img->h;
	rect.w = img->w / 8;
	for (i = 0; i<8; i++) {
		walks[i].w = img->w / 8;
		walks[i].h = img->h;
		walks[i].x = i*walks[i].w;
		walks[i].y = 0;
	}
	i = 0;
 
	while (done == 0) {
		//_getch();
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT) {
				done = 1;
			}//exit
		}//pollevent
		SDL_RenderClear(rend);
		SDL_RenderCopy(rend, texture, &walks[i], &rect);
		i = (i + 1) % 8;
      SDL_Log("move...%d", i);
      //不是原地行走!!
		rect.x = (rect.x + 5) % (rect.w * 8);
       SDL_Log("move...%d, %d", i, rect.x);
		SDL_RenderPresent(rend);
     // SDL_UpdateWindowSurface(win);
		SDL_Delay(SPEED);
	}//done
	SDL_DestroyWindow(win);
	SDL_Quit();
 
	system("pause");
	return 0;
}
